package com.gwgreenway.service;

import com.gwgreenway.dao.UserDao;
import com.gwgreenway.domain.User;
import com.gwgreenway.domain.UserQuery;
import com.gwgreenway.oauth2.common.exception.CheckException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class UserService {

	private static Logger logger = LoggerFactory.getLogger(UserService.class);
	@Resource
	private UserDao userDao;

	public List<User> queryListByPage(UserQuery user) {
		logger.info("access queryListByPage method params is:{}", user.toString());
		return userDao.queryListByPage(user);

	}

	@Transactional
	public void save(User user) {
		logger.info("access get method params is:{}", user.toString());
		if (StringUtils.isBlank(user.getName())) {
			throw new CheckException("用户名不能为空");
		}
		if (user.getAge() == null) {
			throw new CheckException("年龄不能为空");
		}
		if (StringUtils.isBlank(user.getPhone())) {
			throw new CheckException("电话不能为空");
		}
		user.setPassword("123456");
		userDao.save(user);
		//为了验证事务
		//throw new CheckException("添加数据错误");

	}

	public User get(Integer id) {
		logger.info("access get method param is:{}", id);
		return userDao.get(id);
	}

	public void delete(Integer id) {
		logger.info("access delete method param is:{}", id);
		userDao.delete(id);
	}

	public int queryCount(UserQuery user) {
		logger.info("access queryCount method param is:{}", user.toString());
		return userDao.queryCount(user);
	}

}
